// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = wx.cloud.database()

  return new Promise((resolve, reject) => db
    .collection('history')
    .add({
      data: {
        area,
        name,
        message,
        timestamp: Date.now()
      },
      success: res => {
        db
          .collection('history')
          .get()
          .then(res => resolve(res.data.length))
      },
      fail: err => {
        reject(err)
      }
    }
  ))
}